package com.ruoyi.service.card.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.bot.FeishuBot;
import com.ruoyi.service.card.IGshCardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * TODO
 *
 * @author chenchuan2
 * @date 2024-04-05 18:11:31
 */
@Service
@Slf4j
public class GshCardServiceImpl implements IGshCardService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private FeishuBot feishuBot;

    @Override
    public boolean checkInput(String code) {

        return false;
    }

    @Override
    public String gshCardMemberNo() {
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImFmYWFmNjM0MTdmZDQzOTgyMjAwN2E0YmZmMDkzMWIzMzkxN2QwY2FlODZkYTY2ODAyYTU4ZjNjMzhiZDg5ZGQyM2I1MTAzNjIxNDY2Y2Q4In0.eyJhdWQiOiI0IiwianRpIjoiYWZhYWY2MzQxN2ZkNDM5ODIyMDA3YTRiZmYwOTMxYjMzOTE3ZDBjYWU4NmRhNjY4MDJhNThmM2MzOGJkODlkZDIzYjUxMDM2MjE0NjZjZDgiLCJpYXQiOjE3NjI3NTczMjEsIm5iZiI6MTc2Mjc1NzMyMSwiZXhwIjoxNzY1MzQ5MzIwLCJzdWIiOiIxOTQ2MzkiLCJzY29wZXMiOlsiKiJdfQ.BhSMTffA1eHAws920stTd01KiALrKe7-cG1FbVW4UReLv-5_Ex7HtY0G7fTQ0ByzK7CRf35r3tMMzL3ECsQpD-QWBBkKBUNNDkOcxNwA8ZV0gSBSSXbgBOUFa4T0xRI3kkT8cfk7fbgudfBLmtFYUU8h43wYpiOQcysE6NSNgetX6MHR631DoJ9fNf15bAQe3ieKDVbn5AarEarD4nj0ab_4p60-GiZdLoXNuxeFLT9QoQjUaUGX5M4JIVmPze2YThf-Pfe2yMbsHvdwmcLnoIRKSd6KVXEZI1a1GtyoQ-fBmauGV0Tz1wHWyjoafF9xNUddbvs2ybi4jaGOE_X08Hxru7SWEhlRVRWTeC7suRO9cBXFAjX2rVXvXeMW2DrCi5TA-k0VW8EG1-PDIN5ZIwF2hNEFjNPVkIiym2F0_s4V3RSQ3Ma0N3YRaBowittBGAySKDVO3NEhA6uVyxbyyGFQWAcJQ0uJXPQfTLBimCQoA_BHzgUX3_XYXxfbof2INgyJhJzls4lHj2W-5I1QbquXmdCeXU4ErOvuB83cGFms1VM3487I7O8hx03yDQw1ro8NeNWdvWB_C9KEUAnJk_133Rkmb7KM9qsm6Mid2ad6DGs4wSKAX_dS3m4uPXtjyVb0f2FXvznk4VtVLXas3bbMh2tuaQGrSUlglvJlsZs");

        // 设置请求体
        String requestBody = ""; // 如果需要发送请求体，请将其填充为具体的内容

        // 构造请求实体
        HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);

        // 设置请求URL
        String url = "https://vip.wgny.com.cn/shop/v1/memberCard/code";

        // 发送GET请求
        //加入header：Referer：https://servicewechat.com/wx30671158fc69e9df/150/page-frame.html
        headers.add("Referer", "https://servicewechat.com/wx30671158fc69e9df/150/page-frame.html");
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);

        // 获取响应状态码
        HttpStatus statusCode = responseEntity.getStatusCode();

        // 获取响应体
        String responseBody = responseEntity.getBody();

        JSONObject json = JSONObject.parseObject(responseBody);
        String message = json.getString("message");
        if (message.contains("success")) {
            String memberNo = json.getJSONObject("result").getString("member_no");
            try {
                String s = RSADecryptUtil.decryptData(memberNo);
                return s;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            feishuBot.sendFeishuMsg(message);
            throw new RuntimeException("服务卡异常");
        }
    }
}
